﻿using BLL;
using DTO;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HDT.GUI
{
    public partial class FrmThemCTPN : Form
    {
        public delegate void MyDelegate(ChiTietPhieuNhapDTO chiTiet);
        public MyDelegate AddChiTiet;

        public FrmThemCTPN()
        {
            InitializeComponent();
        }

        private void txtMaMH_TextChanged(object sender, EventArgs e)
        {
            try
            {
                LoadMatHangInfo(new MatHangBLL().GetByID(txtMaMH.Text.Trim()));
            }
            catch { }
            btnNhapLai.Enabled = true;
        }

        private void LoadMatHangInfo(MatHangDTO matHang)
        {
            try
            {
                txtMaMH.Text = matHang.MaMatHang;
                txtTenMH.Text = matHang.TenMatHang;
                try
                {
                    txtLMH.Text = new LoaiMatHangBLL().GetByID(matHang.MaLoaiMatHang).TenLoaiMatHang;
                }
                catch
                {
                    txtLMH.Text = "N/A";
                }
                txtDViTinh.Text = matHang.DonViTinh;
                
                dtmHSD.Enabled = matHang.HanSuDung;
                dtmHSD.Value = DateTime.Now;
                numSL.Value = numSL.Minimum;
                numSL.Enabled = true;
                numGia.Value = numGia.Minimum;
                numGia.Enabled = true;
                btnThemCT.Enabled = true;
            }
            catch
            {
                txtTenMH.Clear();
                txtLMH.Clear();
                txtDViTinh.Clear();                
                dtmHSD.Enabled = false;
                numSL.Enabled = false;
                numGia.Enabled = false;
                btnThemCT.Enabled = false;
            }
        }

        private void btnNhapLai_Click(object sender, EventArgs e)
        {
            txtMaMH.Clear();
            btnNhapLai.Enabled = false;
        }

        private void btnQuayLai_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btnThemCT_Click(object sender, EventArgs e)
        {
            ChiTietPhieuNhapDTO chiTiet = new ChiTietPhieuNhapDTO();
            chiTiet.MaMatHang = txtMaMH.Text.Trim();
            if (dtmHSD.Enabled)
            {
                chiTiet.HanSuDung = dtmHSD.Value;
            }
            chiTiet.SoLuongNhap = (short)numSL.Value;
            chiTiet.DonGiaNhap = (int)numGia.Value;
            AddChiTiet(chiTiet);
            this.Dispose();
        }

        private void btnMaMH_Click(object sender, EventArgs e)
        {
            FrmSearchMH f = new FrmSearchMH();
            f.callBack = new FrmSearchMH.MyDelegate(LoadMatHangInfo);
            f.ShowDialog();
        }

        private void txtMaMH_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsControl(e.KeyChar) || char.IsDigit(e.KeyChar) || e.KeyChar == 'm' || e.KeyChar == 'M' || e.KeyChar == 'h' || e.KeyChar == 'H'))
            {
                e.Handled = true;
            }
        }
    }
}
